package netty.ch2;

import jdk.nashorn.internal.runtime.ECMAException;

import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    private ServerSocket serverSocket;

    public Server(int port){

        try{
            this.serverSocket = new ServerSocket(port);
            System.out.println("服务端启动成功,端口:"+port);
        }catch (Exception e){
            System.out.println("服务端启动失败");
        }
    }

    public void start(){
        new Thread(()->{
            doStart();
        }).start();
    }

    public void doStart(){
        while (true){
            try{
                Socket client = serverSocket.accept();
                new ClientHandler(client).start();
            }catch (Exception e){
                System.out.println("服务端异常");

            }
        }
    }

}
